﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Tezzanator.Net.XNAGUI.Controls
{
    public class Window : BaseControl
    {
        public Texture2D BlankPixel;

        public int BorderThickness = 2;
        public int TitleBarHeight = 24;
        public string Title = "Window";
        public bool Movable = false;
        public Color BorderColor = new Color(0,0,0, 120);
        public Color BackgroundColor = new Color(Color.DarkSlateGray.R, Color.DarkSlateGray.G, Color.DarkSlateGray.B, 210);
        public Color TitleBarColor = new Color(20,20,20,230);
        public Color TitleTextColor = Color.White;
        public SpriteFont TitleFont;

        Game game;

        public Window(Game thegame) : base()
        {
            game = thegame;

            // Create a 1x1 textre to draw some primitives.
            Color[] pixelData2 = new Color[1];
            BlankPixel = new Texture2D(game.GraphicsDevice, 1, 1);
            BlankPixel.GetData<Color>(pixelData2);
            pixelData2[0] = Color.White;
            BlankPixel.SetData<Color>(pixelData2);
        }

        public override void Draw(GameTime gameTime, SpriteBatch spritebatch)
        {
            
            if (Visible)
            {
                // Draw the border
                spritebatch.Draw(BlankPixel, new Rectangle(
                    (int)AbsoluteScreenPosition.X - BorderThickness,
                    (int)AbsoluteScreenPosition.Y - BorderThickness,
                    (int)Size.X + (BorderThickness * 2),
                    (int)Size.Y + (BorderThickness * 2)), BorderColor);

                // Draw the inner
                spritebatch.Draw(BlankPixel, new Rectangle(
                    (int)AbsoluteScreenPosition.X,
                    (int)AbsoluteScreenPosition.Y,
                    (int)Size.X,
                    (int)Size.Y), BackgroundColor);

                // Draw Title bar
                spritebatch.Draw(BlankPixel, new Rectangle(
                    (int)AbsoluteScreenPosition.X,
                    (int)AbsoluteScreenPosition.Y,
                    (int)Size.X,
                    (int)TitleBarHeight), TitleBarColor);

                if (TitleFont != null)
                {
                    int y = (int)TitleFont.MeasureString(Title).Y;
                    Vector2 titlepos = new Vector2(AbsoluteScreenPosition.X + 12, (AbsoluteScreenPosition.Y + (TitleBarHeight / 2)) - (y / 2));
                    spritebatch.DrawString(TitleFont, Title, titlepos , TitleTextColor);
                }

                base.Draw(gameTime, spritebatch);
            }
        }

        public override void Update(GameTime gameTime)
        {
            if (Movable)
            {
                // TODO: Window dragging code in here
            }

            base.Update(gameTime);
        }
    }
}
